package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
	"strings"
)

type MyStruct struct {
	String string `validate:"in-hello"` // 使用自定义的校验tag
}

var validate *validator.Validate

func main() {
	validate = validator.New()
	// 注册自定义的校验tag及处理函数
	validate.RegisterValidation("in-hello", ValidateMyVal)
	// 构建测试数据并验证
	s := MyStruct{String: "World"}
	err := validate.Struct(s)
	if err != nil {
		fmt.Printf("Err(s):\n%+v\n", err)
	}

}

// 实现validator.Func自定义验证  验证匹配的字段是否包含hello字符串
func ValidateMyVal(fl validator.FieldLevel) bool {
	return strings.Contains(fl.Field().String(), "hello")
}
